<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
	
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="用户信息" field="nickname">
		<span class="arco-auto-tooltip arco-table-td-content">
		  <span class="arco-auto-tooltip-content">
			<!-- 新增一个容器用于图片和商品名称并排显示 -->
			<div class="image-name-container" style="display: flex; align-items: center;">
			  <img  
				class="time-info goods-image"  
				data-v-49576cc5="" 
				:src="formData.avatar" 
				style="object-fit: cover; cursor: pointer; width: 80px; height: 80px; border-radius: 50%; margin-right: 10px;"
			  >
			  <span class="time-info goods-name">{{formData.nickname}}</span>
			</div>
		  </span>
		</span>
      </a-form-item>
   
      <a-form-item label="供应权重" field="supply_weight">
        <a-input-number v-model="formData.supply_weight" placeholder="请输入供应权重" />
      </a-form-item>
      <a-form-item label="需求权重" field="requirement_weight">
        <a-input-number v-model="formData.requirement_weight" placeholder="请输入需求权重" />
      </a-form-item>
      <a-form-item label="浏览权重" field="browsing_weight">
        <a-input-number v-model="formData.browsing_weight" placeholder="请输入浏览权重" />
      </a-form-item>
      <a-form-item label="收藏权重" field="collection_weight">
        <a-input-number v-model="formData.collection_weight" placeholder="请输入收藏权重" />
      </a-form-item>
      <a-form-item label="对接权重" field="docking_eight">
        <a-input-number v-model="formData.docking_eight" placeholder="请输入对接权重" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/user'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '用户列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  user_id: 1,
  supply_weight: 1,
  requirement_weight: 1,
  browsing_weight: 1,
  collection_weight: 1,
  docking_eight: 1,
})
// 验证规则
const rules = {
  supply_weight: [
    { required: true, message: '供应权重必需填写' },
    { type: 'number', min: 0, message: '供应权重不能小于0' }
  ],
  requirement_weight: [
    { required: true, message: '需求权重必需填写' },
    { type: 'number', min: 0, message: '需求权重不能小于0' }
  ],
  browsing_weight: [
    { required: true, message: '浏览权重必需填写' },
    { type: 'number', min: 0, message: '浏览权重不能小于0' }
  ],
  collection_weight: [
    { required: true, message: '收藏权重必需填写' },
    { type: 'number', min: 0, message: '收藏权重不能小于0' }
  ],
  docking_eight: [
    { required: true, message: '对接权重必需填写' },
    { type: 'number', min: 0, message: '对接权重不能小于0' }
  ]
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.user_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.user_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
